<?php
/**
 * Created by PhpStorm.
 * User: seven
 * Date: 2015/12/22
 * Time: 17:37
 */

namespace Album\Controller;


use Album\Form\AlbumForm;
use Album\Model\Album;
use Application\Controller\BaseController;
use Zend\View\Model\ViewModel;

class AlbumController extends BaseController
{
    protected $albumTable;

    public function indexAction()
    {
        echo $this->getDate();
        return new ViewModel([
                                 'albums' => $this->getAlbumTable()->fetchAll(),
                             ]);
    }

    public function addAction()
    {
        $form = new AlbumForm();
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $album = new Album();
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $album->exchangeArray($form->getData());
                $this->getAlbumTable()->saveAlbum($album);

                return $this->redirect()->toRoute('album');
            }
        }
        return ['form' => $form];
    }

    public function editAction()
    {
        $id = (int)$this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('album', [
                'action' => 'add'
            ]);
        }

        try {
            $album = $this->getAlbumTable()->getAlbum($id);
        } catch (\Exception $ex) {
            return $this->redirect()->toRoute('album', [
                'action' => 'index'
            ]);
        }

        $form = new AlbumForm();
        $form->bind($album);
        $form->get('submit')->setAttribute('value', 'Edit');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $this->getAlbumTable()->saveAlbum($album);

                return $this->redirect()->toRoute('album');
            }
        }

        return [
            'id'   => $id,
            'form' => $form
        ];

    }

    public function deleteAction()
    {
        $id = (int)$this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('album');
        }

        $request = $this->getRequest();
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                $id = (int)$request->getPost('id');
                $this->getAlbumTable()->deleteAlbum($id);
            }

            return $this->redirect()->toRoute('album');
        }

        return [
            'id'    => $id,
            'album' => $this->getAlbumTable()->getAlbum($id)
        ];
    }

    public function getAlbumTable()
    {
        if (!$this->albumTable) {
            $sm               = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }
}